package com.book.bigwork.Webservice;

import com.book.bigwork.model.Roles;
import com.book.bigwork.model.User;
import com.book.bigwork.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class UserService {
    private final UserRepository userRepository;
    private final RolesRepository rolesRepository;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    private final OrderRepository orderRepository;

    @Autowired
    public UserService(UserRepository userRepository,
                       RolesRepository rolesRepository,
                       BCryptPasswordEncoder bCryptPasswordEncoder,
                       OrderRepository orderRepository){
        this.userRepository = userRepository;
        this.rolesRepository = rolesRepository;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
        this.orderRepository = orderRepository;
    }
    public User getUserByUsername(String username){
        return userRepository.findByUserName(userName);
    }

    public User registerUser(User user) {
        User newUser = new User();
        Roles role = rolesRepository.findById("ROLE_USER").get();
        Set<Roles> roles = new HashSet<>();
        String encryptedPassword =
                bCryptPasswordEncoder.encode(user.getUserPassword());
        newUser.setUserName(user.getUserName());
        newUser.setUserPassword(encryptedPassword);
        newUser.setUserFullname(user.getUserFullname());
        newUser.setUserPhone(user.getUserPhone());
        newUser.setUserAddress(user.getUserAddress());
        newUser.setUserBankcard(user.getUserBankcard());
        roles.add(role);
        newUser.setRoles(roles);
        userRepository.save(newUser);
        return newUser;
    }
    public User createUser(UserForm userForm) {
        User user = userForm.toUser();
        return user;
    }
    public void updateUser(User user) {
        User updatedUser = userRepository.findById(user. getUserId()).
                get();
        updatedUser.setUserFullname(user.getUserFullname());
        updatedUser.setUserPhone(user.getUserPhone());
        updatedUser.setUserAddress(user.getUserAddress());
        updatedUser.setUserBankcard(user.getUserBankcard());
        userRepository.save(updatedUser);
    }
    public List<User> findAllUsers() {
        return userRepository.findAll();
    }
    public User findUserById(Long userId) {
        return userRepository.findById(userId).get();
    }
    public void deleteUserById(Long userId) {
        userRepository.deleteById(userId);
    }
    public int getUserOrderAmount(Long userId) {
//查询用户对应的订单数量，有订单号的用户不允许删除
        final User user;
        final List<Order> order;
        user = userRepository.findById(userId).get();
        order = orderRepository.findByUser(user);
        return order.size();
    }
}
